﻿using dotNetMagazin201108.BusinessLayer;
using System.Collections.Generic;
using Microsoft.SharePoint.Client;

namespace dotNetMagazin201108.PersistenceLayer
{
    public class SharePointContactRepository : SharePointBaseRepository, ContactRepository
    {
        private string SharePointListNameCompanies = "Contacts";

        public IEnumerable<Contact> GetAll()
        {
            var context = CurrentContext;

            List companies = context.Web.Lists.GetByTitle(SharePointListNameCompanies);
            ListItemCollection items = companies.GetItems(new CamlQuery());
            //context.Load(items, i => i);
            context.Load(items);
            context.ExecuteQuery();

            foreach (ListItem i in items)
            {
                yield return new Contact() 
                { 
                    Id = i.Id,
                    CompanyId = ((FieldLookupValue)i["Company"]).LookupId, 
                    LastName = (string)i["Title"],
                    FirstName = (string)i["FirstName"],
                    Phone = (string)i["Phone"],
                    Email = (string)i["Email"]
                };
            }
        }

        public Contact GetById(int id)
        {
            var context = CurrentContext;

            List companies = context.Web.Lists.GetByTitle(SharePointListNameCompanies);
            CamlQuery query = new CamlQuery();
            string camlQueryXml = "<View><Query><Where>" +
                                    "<Eq>" +
                                     "<FieldRef Name='ID' />" +
                                     "<Value Type='Number'>"+ id +"</Value>" +
                                  "</Eq>" +
                                "</Where></Query><ViewFields>" +
                                "<FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" />"+
                                "<FieldRef Name=\"FirstName\" /><FieldRef Name=\"Phone\" />" +
                                "<FieldRef Name=\"Email\" />" +
                                "<FieldRef Name=\"Company\" /></ViewFields></View>";
            query.ViewXml = camlQueryXml;

            ListItemCollection items = companies.GetItems(query);
            context.Load(items);
            context.ExecuteQuery();

            ListItem item = items[0];

            return new Contact()
            {
                Id = item.Id,
                CompanyId = ((FieldLookupValue)item["Company"]).LookupId,
                LastName = (string)item["Title"],
                FirstName = (string)item["FirstName"],
                Phone = (string)item["Phone"],
                Email = (string)item["Email"]
            };
        }
        

        public bool Add(Contact entity)
        {
            var context = CurrentContext;
            List contacts = context.Web.Lists.GetByTitle(SharePointListNameCompanies);
            ListItemCreationInformation listCI = new ListItemCreationInformation();

            ListItem contact = contacts.AddItem(listCI);
            UpdateListItem(entity, contact);
            context.ExecuteQuery();

            entity.Id = contact.Id;

            return true;
        }

        public bool Update(Contact entity)
        {
            var context = CurrentContext;
            List contacts = context.Web.Lists.GetByTitle(SharePointListNameCompanies);
            ListItemCreationInformation listCI = new ListItemCreationInformation();

            ListItem contact = contacts.GetItemById(entity.Id);
            UpdateListItem(entity, contact);
            context.ExecuteQuery();

            return true;
        }

        private static void UpdateListItem(Contact entity, ListItem contact)
        {
            contact["Title"] = entity.LastName;
            contact["Company"] = entity.CompanyId;
            contact["FirstName"] = entity.FirstName;
            contact["Phone"] = entity.Phone;
            contact["Email"] = entity.Email;
            contact.Update();
        }
    }
}
